package com.zh.baselibrary.data;

import com.zh.model.manager.BookInfo;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.io.IOException;
import java.util.List;

/**
 * @author huhui
 */
public class DBDataUtils {


    public static int getCount(Class<?> entityType) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        int count = 0;
        try {
            List list = db.findAll(entityType);
            if (list != null) {
                count = list.size();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return count;
    }

    public static int getCount(Class<?> entityType, String key, String value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        int count = 0;
        try {
            List list = db.selector(entityType)
                    .where(key, "=", value)
                    .findAll();
            if (list != null) {
                count = list.size();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 获取数据
     *
     * @param entityType
     * @return
     */
    public static Object getInfos(Class<?> entityType) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.findAll(entityType);
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfo(Class<?> entityType, String key, String value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object info = null;
        try {
            info = db.selector(entityType).where(key, "=", value).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfoLike(Class<?> entityType, String key, String value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object info = null;
        try {
            info = db.selector(entityType).where(key, "like", value).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfo(Class<?> entityType, String key, int value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object info = null;
        try {
            info = db.selector(entityType).where(key, "=", value).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfo(Class<?> entityType, String key, long value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object info = null;
        try {
            info = db.selector(entityType).where(key, "=", value).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 获取数据
     *
     * @return
     */
    public static Object getInfos(Class<?> entityType, String orderBy) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType)
                    .orderBy(orderBy, true)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    public static Object getInfos(Class<?> entityType, String orderBy,
                                  int pageSize) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType)
                    .orderBy(orderBy, true)
                    .limit(pageSize)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }


    public static Object getInfosOrder(Class<?> entityType, String orderBy) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType)
                    .orderBy(orderBy, true)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfos(Class<?> entityType, String orderBy,
                                  String key, String value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType).where(key, "=", value)
                    .orderBy(orderBy, true)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    public static Object getInfos(Class<?> entityType, String key, String value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType).where(key, "=", value).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfos(Class<?> entityType, String orderBy,
                                  String key, int value, String key1, int value1) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType).where(key, "=", value)
                    .and(WhereBuilder.b(key1, "=", value1))
                    .orderBy(orderBy, true)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    /***
     * 根据账号获取聊天消息
     *
     * @param entityType
     * @param key
     * @param value
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Object getMessageLogs(Class<?> entityType, String key,
                                        String value, String key1, int value1) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType)
                    .where(key, "=", value)
                    .and(WhereBuilder.b(key1, "=", value1))
                    .orderBy("times", true)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 获取数据
     *
     * @param key
     * @param value
     * @return
     */
    public static Object getInfos(Class<?> entityType, String key, boolean value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        Object list = null;
        try {
            list = db.selector(entityType)
                    .where(key, "=", value)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 删除数据
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean deleteInfo(Class<?> entityType, String key,
                                     String value) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        int result = -1;
        try {
            result = db.delete(entityType, WhereBuilder.b(key, "=", value));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return result == 1;
    }

    /**
     * 删除全部数据
     *
     * @return
     */
    public static boolean deleteInfo(Class<?> entityType) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        boolean isSuccess = false;
        try {
            db.delete(entityType);
            isSuccess = true;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    /***
     * 删单条数据
     *
     * @param obj
     */
    public static void deleteInfo(Object obj) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        try {
            db.delete(obj);
        } catch (DbException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public static void save(Object obj) {
        try {
            DbManager dbUtils = DataBaseCreator.getInstance().getDbManager();
            dbUtils.save(obj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    public static void searchAndUp(BookInfo obj) {
//        try {
//            DbManager dbUtils = DataBaseCreator.getInstance().getDbManager();
//            WhereBuilder b = WhereBuilder.b();
//            b.and("book_id","=",obj.getBookId()); //构造修改的条件
//           BookInfo bookInfo =   dbUtils.selector(BookInfo.class).where(b).findFirst();
//           if(bookInfo == null)
//               dbUtils.save(obj);
//           else {
//               bookInfo.setStatus(obj.getStatus());
//               dbUtils.update(bookInfo,"status" );
//           }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


    public static void dropTable(Class<?> entityType) {
        try {
            DbManager dbUtils = DataBaseCreator.getInstance().getDbManager();
            dbUtils.dropTable(entityType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void updateOrSave(Object entity) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        try {
            db.saveOrUpdate(entity);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public static void update(Object obj) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        try {
            db.update(obj);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    public static void updateKey(Class<?> entityType, String key) {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        try {
            db.update(entityType, WhereBuilder.b(key, "=", key));

        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    /**
     * 插入全部对象
     *
     * @param entity 实体类的对象
     * @return true:插入成功 false:插入失败
     */
    public static synchronized boolean saveAll(Object entity) {
        try {
            DbManager db = DataBaseCreator.getInstance().getDbManager();
            db.save(entity);
        } catch (DbException e) {
            if (e != null)
                e.printStackTrace();
            return false;
        }
        return true;
    }


    public void closeDB() {
        DbManager db = DataBaseCreator.getInstance().getDbManager();
        try {
            db.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
